iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 4
2
自我挑戰組

TensorFlow 2 30天自我養成計畫系列 第 4

[Day 4]Colab,淺顯又易懂!

  • 分享至 

  • xImage
  •  
其實,我們這幾天所做的所有環境建置的努力,是可以有更簡潔的辦法。
是什麼呢?我們今天好好介紹!

Colab
Colab是由Google所提供的雲端運行環境,其支援Python程式及機器學習TensorFlow演算法。為什麼會推薦這個呢?主要是因為筆者雖然有足夠的硬體設備可以執行TensorFlow等程式,但多半的人不一定擁有足夠的設備,這時有個雲端平台可以使用是再好不過了!

圖片來源:https://www.facebook.com/GoogleColab/

Colab的優點是只需要瀏覽器(Chrome、Firefox或Safari皆可使用)就可以運作,且完全免費!

Colab目的在提供Machine Learning機器學習訓練,不須下載與安裝就可直接應用Python 2.7 與 Python 3.6資源庫,對初學者的我來說不但可以快速入門,也不需耗時間在環境設定上。程式碼預設會直接儲存在開發者的Google Drive雲端硬碟中。

介紹完後,開始進入Colab的環境建置,真的很簡單/images/emoticon/emoticon08.gif!

Colab 環境建置

  1. 首先,進入Colab(網址:https://colab.research.google.com/notebooks/intro.ipynb#recent=true )後,新建記事本
  2. 輸入程式碼如下
    import tensorflow as tf ,並執行
    就會出現目前狀態,如下圖:
  3. 上述結束後,按左上角的+code鍵,輸入以下程式碼:
    tf._version_,再繼續執行,會得到Colab中的TensorFlow版本,如下圖:

    筆者的版本為2.3.0。
  4. 接著繼續按下+code鍵,繼續輸入以下網址:
    !pip install tensorflow-gpu==2.0.0-beta1,結果如下所示:
Collecting tensorflow-gpu==2.0.0-beta1
  Downloading https://files.pythonhosted.org/packages/2b/53/e18c5e7a2263d3581a979645a185804782e59b8e13f42b9c3c3cfb5bb503/tensorflow_gpu-2.0.0b1-cp36-cp36m-manylinux1_x86_64.whl (348.9MB)
     |████████████████████████████████| 348.9MB 27kB/s 
Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.12.1)
Collecting tf-estimator-nightly<1.14.0.dev2019060502,>=1.14.0.dev2019060501
  Downloading https://files.pythonhosted.org/packages/32/dd/99c47dd007dcf10d63fd895611b063732646f23059c618a373e85019eb0e/tf_estimator_nightly-1.14.0.dev2019060501-py2.py3-none-any.whl (496kB)
     |████████████████████████████████| 501kB 30.0MB/s 
Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.15.0)
Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (0.2.0)
Requirement already satisfied: gast>=0.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (0.3.3)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.32.0)
Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (0.8.1)
Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (0.35.1)
Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (0.10.0)
Requirement already satisfied: numpy<2.0,>=1.14.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.18.5)
Collecting tb-nightly<1.14.0a20190604,>=1.14.0a20190603
  Downloading https://files.pythonhosted.org/packages/a4/96/571b875cd81dda9d5dfa1422a4f9d749e67c0a8d4f4f0b33a4e5f5f35e27/tb_nightly-1.14.0a20190603-py3-none-any.whl (3.1MB)
     |████████████████████████████████| 3.1MB 34.0MB/s 
Collecting keras-applications>=1.0.6
  Downloading https://files.pythonhosted.org/packages/71/e3/19762fdfc62877ae9102edf6342d71b28fbfd9dea3d2f96a882ce099b03f/Keras_Applications-1.0.8-py3-none-any.whl (50kB)
     |████████████████████████████████| 51kB 5.8MB/s 
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.1.0)
Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (3.12.4)
Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow-gpu==2.0.0-beta1) (1.1.2)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0.0-beta1) (1.0.1)
Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0.0-beta1) (50.3.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0.0-beta1) (3.2.2)
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.6->tensorflow-gpu==2.0.0-beta1) (2.10.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.6/dist-packages (from markdown>=2.6.8->tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0.0-beta1) (1.7.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < "3.8"->markdown>=2.6.8->tb-nightly<1.14.0a20190604,>=1.14.0a20190603->tensorflow-gpu==2.0.0-beta1) (3.1.0)
Installing collected packages: tf-estimator-nightly, tb-nightly, keras-applications, tensorflow-gpu
Successfully installed keras-applications-1.0.8 tb-nightly-1.14.0a20190603 tensorflow-gpu-2.0.0b1 tf-estimator-nightly-1.14.0.dev2019060501

結束後按最下方的Restart Runtime,再次查看版本,結果如下圖

注意!請如果需要GPU的使用,請需按照以下步驟:

請點選GPU,如下:

這樣Colab環境建置就成功啦!明天再接再勵/images/emoticon/emoticon12.gif

Reference: http://www.cc.ntu.edu.tw/chinese/epaper/0052/20200320_5207.html
Reference: https://tf.wiki/zh_hant/appendix/cloud.html
Reference: https://medium.com/@FishFu/%E7%AC%AC%E4%B8%80%E6%AC%A1%E7%94%A8-google-colab-%E5%B0%B1%E4%B8%8A%E6%89%8B-1dd3291c7612
Reference: https://colab.research.google.com/notebooks/intro.ipynb
Reference: 李金洪(2020):極詳細+超深入:最新版TensorFlow 1.x/2.x完整工程實作。台北市:深智數位


上一篇
[Day 3]環境建置 Part 2
下一篇
[Day 5]AI,你行不行? Part 1
系列文
TensorFlow 2 30天自我養成計畫30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言